package cn.tedu.egmvc4.controller;

import cn.tedu.egmvc4.mapper.UserMapper;
import cn.tedu.egmvc4.pojo.dto.UserAddParam;
import cn.tedu.egmvc4.pojo.dto.UserUpdateParam;
import cn.tedu.egmvc4.pojo.entity.User;
import cn.tedu.egmvc4.pojo.vo.UserListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/user")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @PostMapping("/add")
    public String add(UserAddParam userAddParam){
        System.out.println(userAddParam);
       User user =  new User();
       BeanUtils.copyProperties(userAddParam,user);
       user.setUpdateTime(new Date());
       user.setCreateTime(new Date());
      userMapper.insert(user);
      return "添加成功";
    }
    @RequestMapping("/list")
    public List<UserListVO> list(){
       return userMapper.list();
    }
    @RequestMapping("/delete")
    public String delete(Long id){
      int num =  userMapper.deleteById(id);
        if (num>0){
            return "删除成功";
        }else {
            return "删除失败";
        }
    }

    @RequestMapping("/update")
    public String update(UserUpdateParam userUpdateParam){
        User user = new User();
        BeanUtils.copyProperties(userUpdateParam,user);
        user.setUpdateTime(new Date());
        int num = userMapper.update(user);
        return num>0?"修改成功":"修改失败";
    }
}
